package testfour;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Telephone {
    private String startingTime;
    private String endingTime;
    private long minute; //通话时长
    private Date startT;
    private Date endT;
    public  Telephone(final String startingTime, final String endingTime) {
        this.startingTime = startingTime;
        this.endingTime = endingTime;
        this.startT = strToDate(this.startingTime);
        this.endT = strToDate(this.endingTime);
    }

    public Date strToDate(final String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HHmmss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
//    public String dateFormate(final Date date) {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss E");
//        String sdate = format.format(date);
//        return sdate;
//    }
    public long getTelephone() {
        long between;
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(startT);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endT);

        between = cal2.getTime().getTime() - cal1.getTime().getTime();

        final int a = 1000;
        final int b = 60;
        final int monthStart = 7;
        final int monthEnd = 31;
        final int hour1 = 2;
        final int hour2 = 3;
        final int hour3 = 1800;
        if (between < 0) {
            minute = ((cal2.getTime().getTime() - cal1.getTime().getTime()) / a) / b; //得到通话时长
        } else {
            minute = (between / a + b - 1) / b;
        }
        //判断接通时间是否在发生转换的日期
        if ((cal1.get(Calendar.MONTH) == Calendar.MARCH && cal1.get(Calendar.DATE) >= 1
                && cal1.get(Calendar.DATE) <= monthEnd
                && cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
            if (cal1.get(Calendar.HOUR_OF_DAY) < 2 && cal2.get(Calendar.HOUR_OF_DAY) >= hour2) {
                minute -= b; //时间从2点转换到3点减去1h
            }
        } else {
            if ((cal2.get(Calendar.MONTH) == Calendar.MARCH && cal2.get(Calendar.DATE) >= 1
                    && cal2.get(Calendar.DATE) <= monthEnd && cal2.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
                if (cal2.get(Calendar.HOUR_OF_DAY) == hour2) {
                    minute -= b;
                }
            }
        }
        //将位于转换的时间段内的时间都看作已经转换之后的
        if (cal1.get(Calendar.MONTH) == Calendar.NOVEMBER && cal1.get(Calendar.DATE) >= 1
                && cal1.get(Calendar.DATE) <= monthStart && cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            if (cal1.get(Calendar.HOUR_OF_DAY) == hour1) {
                minute += b;
            }
        } else {
            if (cal2.get(Calendar.MONTH) == Calendar.NOVEMBER && cal2.get(Calendar.DATE) >= 1
                    && cal2.get(Calendar.DATE) <= monthStart && cal2.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                if (cal2.get(Calendar.HOUR_OF_DAY) == hour1) {
                    minute += b;
                }
            }
        }
        if (minute <= hour3 && minute >= 0) {
            return minute;
        }
        return 0;
    }
}


